package com.example.teesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teesystem.common.utils.FileUtil;
import com.example.teesystem.common.utils.Response;
import com.example.teesystem.common.utils.ResultCode;
import com.example.teesystem.common.utils.Url_ImageUtil;
import com.example.teesystem.entity.PaperFeedback;
import com.example.teesystem.entity.vo.paperFeedback.PaperFeedbackInsertVo;
import com.example.teesystem.entity.vo.paperFeedback.PaperFeedbackUpdateVo;
import com.example.teesystem.filter.MyHandlerInterceptor;
import com.example.teesystem.mapper.*;
import com.example.teesystem.service.PaperFeedbackService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;


/**
* @author 12510
* @description 针对表【t_paper_feedback】的数据库操作Service实现
* @createDate 2022-04-27 19:58:38
*/
@Service
@RequiredArgsConstructor
public class PaperFeedbackServiceImpl extends ServiceImpl<PaperFeedbackMapper, PaperFeedback>
    implements PaperFeedbackService{

    private final ExamEvaluationMapper examEvaluationMapper;
    private final PaperFeedbackMapper paperFeedbackMapper;

    public Response getPaperFeedBackByPaperIdAndCourseId(int paperId, int courseId, int isHandle){
        QueryWrapper<PaperFeedback> wrapper = new QueryWrapper<>();
        wrapper.eq("paper_id", paperId)
                .eq("course_id", courseId)
                .eq("is_handle" , isHandle);
        List<PaperFeedback> paperFeedbacks = this.list(wrapper);
        return Response.ok(paperFeedbacks);
    }

    public Response PaperFeedbackInsert(PaperFeedbackInsertVo paperFeedbackInsertVo){

        //判断学生是否使用过该张试卷
        if(examEvaluationMapper.getEvaluationByPaperAndUserId(paperFeedbackInsertVo.getPaperId(), MyHandlerInterceptor.getUid()) == null) {
            Response.error(ResultCode.PAPER_FEEDBACK_STUDENT_ERROR.getMsg(),ResultCode.PAPER_FEEDBACK_STUDENT_ERROR.getCode());
        }
        //通过资格验证，开始上传paperId,message,courseId,studentId,ID自动自增生成
        PaperFeedback paperFeedback = new PaperFeedback();
        MultipartFile[] files = paperFeedbackInsertVo.getFiles();

        BeanUtils.copyProperties(paperFeedbackInsertVo, paperFeedback);
        paperFeedback.setStudentId(MyHandlerInterceptor.getUid());//studentId
        paperFeedback.setIsHandle(0);

        //判断文件是否有效且是否指向了一个图片
        if(paperFeedbackInsertVo.getFiles() != null){
            for(MultipartFile file:files){
                File file1 = Url_ImageUtil.MultipartFileToFile(file);
                try{
                    if(!(Url_ImageUtil.IsImage(file1))){
                        return Response.error(ResultCode.PAPER_FEEDBACK_IMAGE_FILE_ERROR.getMsg(),ResultCode.PAPER_FEEDBACK_IMAGE_FILE_ERROR.getCode());
                    }
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }

            Response response = FileUtil.UploadFile(files,FileUtil.FILE_PATH+FileUtil.EXAM_FEEDBACK+paperFeedback.getPaperId()+"/");
            String url = response.get("data").toString();
            url = url.substring(url.indexOf("url=")+4, url.indexOf("$}"));
            //判断文件后缀
            if(!(Url_ImageUtil.ValidImgType(url))){
                return Response.error(ResultCode.PAPER_FEEDBACK_URL_ERROR.getMsg(),ResultCode.PAPER_FEEDBACK_URL_ERROR.getCode());

            }
            paperFeedback.setUrl(url);
        }
        this.save(paperFeedback);
        return Response.ok();

    }

    public Response PaperFeedbackHandle(PaperFeedbackUpdateVo paperFeedbackUpdateVo){
        PaperFeedback paperFeedback = paperFeedbackMapper.selectById(paperFeedbackUpdateVo.getId());
        if(paperFeedback == null) {
            return Response.error(ResultCode.PAPER_FEEDBACK_NOT_FOUND_ERROR.getMsg(),ResultCode.PARAMETER_MISS.getCode());
        }
        paperFeedback.setHandleMessage(paperFeedbackUpdateVo.getHandleMessage());
        paperFeedback.setTeacherId(MyHandlerInterceptor.getUid());
        paperFeedback.setIsHandle(1);
        paperFeedbackMapper.updateById(paperFeedback);
        return Response.ok();
    }
}




